/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.service.battle.coreBattleLogic.soulCardLogic;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.hardCode.playerGameStatus;
import com.dc.gameserver.proto.gameserverprotoVo.dizzniessResponse;
import com.dc.gameserver.serverCore.model.gamemodel.EquipmentModel;
import com.dc.gameserver.serverCore.model.gamemodel.ItemCell;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.battle.coreBattleLogic.AbstractSoulLogic;
import com.dc.gameserver.serverCore.service.cardModule.CardBox;
import com.dc.gameserver.serverCore.service.cardModule.CardBoxManager;
import com.dc.gameserver.serverCore.service.character.GameCharacter;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import org.springframework.stereotype.Service;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:14-1-3 </br>
 *         Time:下午2:42 </br>
 *         Package:{@link com.dc.gameserver.serverCore.service.battle.coreBattleLogic.soulCardLogic}</br>
 *         Comment： 0.眩晕
 */
@Service
public class soulDizzinessLogic extends AbstractSoulLogic {

    /**
     * 实例化处理单个玩家出牌逻辑引用
     */
    @Override
    public void PostConstruct() {
        SOULS[0x0]=this;
    }
    /**
     * 处理不同神识牌对象
     * @param p1  使用神牌的对象
     * @param p2
     */
    @Override
    public boolean SoulHandle(GameCharacter p1, GameCharacter p2){
        boolean status=false;
        double euqAdditional=0; //神识法宝
        if (p1 instanceof PlayerInstance){
            PlayerInstance player= (PlayerInstance) p1;
            ItemCell cell=player.getCellByLocation(0x12);//location is 18
            int equTypeID= cell.getItemTypeID();   //
            //如果穿戴了 那么会增加概率
            if (equTypeID!=0&&cell.getType()== 0x8){
                //神识法宝对象  装备对象
               EquipmentModel equipmentModel= ScriptFactory.equipmentModelFastMap.get(equTypeID);
//              <effectType ID="701" intro="穿戴后使用神牌增加眩晕概率"/>
               if(equipmentModel!=null){
                 for (;;){
                     int effectTypeID=equipmentModel.getEffectType1();
                     if (effectTypeID== 0x2bd){
                         euqAdditional=equipmentModel.getEffectPercent1();
                         break;
                     }
                     effectTypeID=equipmentModel.getEffectType2();
                     if (effectTypeID== 0x2bd){
                         euqAdditional=equipmentModel.getEffectPercent2();
                         break;
                     }
                     effectTypeID=equipmentModel.getEffectType3();
                     if (effectTypeID== 0x2bd){
                         euqAdditional=equipmentModel.getEffectPercent3();
                         break;
                     }else {
                         // 配置文件有问题，导致防止死循环
                         break;
                     }
                 }
               }
            }
        }
        if (GameFormula.soulDizzinessSuccess(p1.getSoul(),p2.getSoul(),p1.getOutCardsList().size(),euqAdditional)){
            status=true;
        }
        if (status){
            //notify dizziness
            //p1 神识判断成功，p2眩晕
            CardBox cardBox= CardBoxManager.getCardBoxByID(p1.getCardBoxID());
            int dizzinessTime= Config.DEFAULT_VALUE.GAME_VALUE.DizzinessTimes;
            p2.setDizzinessTimes(--dizzinessTime); //当前回合眩晕，player
            dizzniessResponse.Builder builder=dizzniessResponse.newBuilder();
            builder.setResult(playerGameStatus.dizziness.getStatus());
            builder.setPlayerID(p2.getID());//眩晕对象
            cardBox.submitMessageToEveryOne(wrappedBufferShort(dizzniessResponse.msgID.ID_VALUE, builder.build()));
            cardBox.flushBloodAndMagic();
            cardBox.resetRoundResource();
        }
        return status;
    }
}
